package com.eliansun.crawler.downloader;

import net.dongliu.requests.RawResponse;
import net.dongliu.requests.Requests;
import net.dongliu.requests.Session;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.PlainText;

import java.util.Map;

/**
 * @author ljh
 * @create 2017-08-04 11:09
 **/
public class RequestsDownLoad implements Downloader {

    private Logger logger = Logger.getLogger(getClass());

    public String getMailUrl() {
        return mailUrl;
    }

    public RequestsDownLoad(String mailUrl) {
        this.mailUrl = mailUrl;
    }

    public void setMailUrl(String mailUrl) {
        this.mailUrl = mailUrl;
    }

    private String mailUrl;


    @Override
    public Page download(Request request, Task task) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Session session = Requests.session();
        if (mailUrl.startsWith("https")){
            session.get(mailUrl).verify(false).send();
        } else {
            session.get(mailUrl).send();
        }
        String url = request.getUrl();
        Map<String,Object> headers = new HashedMap();
        headers.put("Content-Encoding","gzip");
        headers.put("Content-Language","zh-CN");
        headers.put("Content-Type","text/html;charset=UTF-8");
        headers.put("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
        RawResponse resp = null;
        if (mailUrl.startsWith("https")) {
             resp = session.get(url).verify(false).headers(headers).send();
        } else {
            resp =  session.get(url).headers(headers).send();
        }
        String html = resp.readToText();
        int code = resp.getStatusCode();
        Page page = new Page();
        page.setUrl(new PlainText(request.getUrl()));
        page.setRequest(request);
        Html html1 = new Html(html);
        page.setHtml(html1);
        page.setStatusCode(code);
        page.setRawText(html);
        resp.close();
        return page;
    }

    @Override
    public void setThread(int i) {

    }
}
